<?php
/**
 * @Author: Awe
 * @Date:   2016-05-15 23:49:50
 * @Last Modified by:   Awe
 * @Last Modified time: 2019-03-26 14:55:00
 * @des 后台的订单 业务文件
 */
class OrderBusiness extends AbstractModel {
    public function getOrderList($params = array(),$pageSize ){
        $page = isset($params['page']) ? $params['page'] : 1 ;
        $getWhere = " where 1 = 1  ";
        $getWhere.=$this->getUserWhere($params);
        $sql = "select a.*  from pay_order as a   {$getWhere} order by a.create_date desc "   ;
        $sql_count = "select count(*) as c   from pay_order as a  {$getWhere} " ;
        return $this->getCommonDataList($sql , $sql_count , $pageSize,$page,0);
    }
    public function getUserWhere($params=array()){
        $getWhere = "";
        if(isset($params['order_id']) && $params['order_id']){
            $getWhere.=" and a.`order_id` = '{$params['order_id']}' ";
        }
        if(isset($params['cid']) && $params['cid']){
            $getWhere.=" and a.`customer_id` = '{$params['cid']}' ";
        }
        if(isset($params['status']) AND in_array($params['status'] ,array(0,1,2) )  ){
            $getWhere.=" and a.`status` = '{$params['status']}' ";
        }
        return $getWhere;
    }
    public function orderTotal($customer_id){
        $today = date("Y-m-d" ,time());
        $yestady = date("Y-m-d" , strtotime("-1 days "));
        $sql = "select sum(money) as money from  pay_order where customer_id = '{$customer_id}' and date_format(`create_date` , '%Y-%m-%d' ) = '{$today}' and status = '1' and rsync_success = '1'  limit 1  ";
        $info = $this->db(0)->findOne($sql);
        $t_count = isset($info['money']) ? $info['money'] : 0 ;

        $sql = "select sum(money) as money from  pay_order where customer_id = '{$customer_id}' and date_format(`create_date` , '%Y-%m-%d' ) = '{$yestady}' and status = '1' and rsync_success = '1'  limit 1  ";
        //echo $sql;
        $info = $this->db(0)->findOne($sql);
        $y_count = isset($info['money']) ? $info['money'] : 0 ;
        return array(
            't_count' => $t_count , 
            'y_count'=>$y_count
        );
    }
}